import { createSlice } from "@reduxjs/toolkit";

const appSlice = createSlice({
  name: "app",

  initialState: {
    // 用于控制左侧菜单的是否收起
    // collapsed: false,
    collapsed: window.localStorage.getItem("collapsed") ? true : false,
  },

  reducers: {
    setCollapsed(state, action) {
      state.collapsed = action.payload;

      // 判断 state.collapsed 为 true 就存起来
      if (state.collapsed) {
        window.localStorage.setItem("collapsed", "true");
      }
    },
  },
});

// 具名导出 reducer
export const { reducer } = appSlice;

// 具名到 actionCreator
export const { setCollapsed } = appSlice.actions;
